package ru.urfc.portal.model.radio;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import ru.urfc.portal.exceptions.UserGetItemException;
import ru.urfc.portal.model.SpectrObject;
import ru.urfc.portal.model.catalogs.NetLicenseState;
import ru.urfc.portal.ui.client.Constants;

@Table(name = "portal_frequencylicenses")
@Entity
@XmlRootElement(name = Constants.LICENSE)
public class NetLicense extends SpectrObject {
	private NetWithLicense net;
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "netLicenseState_code")
	public NetLicenseState state;
	public String number;
	@Temporal(TemporalType.DATE)	
	public Date issueDate;
	@Temporal(TemporalType.DATE)	
	public Date expireDate;

	public NetLicense() {
	}

	public NetLicense(NetWithLicense net, String number, Date issueDate, Date expireDate, NetLicenseState state) {
		this.net = net;
		this.state = state;
		this.number = number;
		this.issueDate = issueDate;
		this.expireDate = expireDate;
	}

	@XmlTransient
	public NetWithLicense getNet() {
		return net;
	}

	public NetLicense check(Long netId) {
		if (net.getId().equals(netId)) {
			return this;
		}
		throw new UserGetItemException(NetLicense.class);
	}
	
}
